package com.lkd.service.impl;


import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.constant.StatusConstant;
import com.lkd.dto.LoginDto;
import com.lkd.dto.UserPageDto;
import com.lkd.entity.StatusType;
import com.lkd.entity.Role;
import com.lkd.exception.BusinessException;
import com.lkd.mapper.RoleMapper;
import com.lkd.mapper.UserMapper;
import com.lkd.result.PageBean;
import com.lkd.service.UserService;
import com.lkd.entity.User;
import com.lkd.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    //用户登录
    @Override
    public User login(LoginDto dto) {
        //参数校验
        String loginName = dto.getLoginName();
        String password = dto.getPassword();
        if (StrUtil.isBlank(loginName)||StrUtil.isBlank(password)) {
            throw new BusinessException("用户名或账号不存在");
        }

        //根据用户名查询数据库
        User user=userMapper.login(loginName);
        //业务校验
        //账号是否存在
        if (user==null) {
            throw new BusinessException("用户名不存在");
        }
        //密码是否正确
        String md5 = SecureUtil.md5(password);
        if (!StrUtil.equals(password,user.getPassword())) {
            throw new BusinessException("密码错误");
        }
        if (user.getStatus().equals(StatusConstant.DISABLE)) {
            throw new BusinessException("此账号被禁用");
        }
        return user;
    }

    @Override
    //用户分页
    public PageBean search(UserPageDto userPageDto) {
        PageHelper.startPage(userPageDto.getPageIndex(),userPageDto.getPageSize());
        //查询user表
        List<UserVo> userVos = userMapper.search(userPageDto);
        userVos.forEach(userVo ->
                {
                    userVo.setPassword("******");
                    Role role = roleMapper.getById(userVo.getRoleId());
                    userVo.setRole(role);
                }
        );
        Page<UserVo> page = (Page<UserVo>) userVos;
        return new PageBean(page.getPageNum(),page.getPageSize(), (long) page.getPages(),page.getTotal(),page.getResult());
    }

    //用户添加
    @Override
    public Boolean save(User user) {
        //补齐参数
        String roleCode = roleMapper.getRoleCodeById(user.getRoleId());
        user.setRoleCode(roleCode);
        if (user.getStatus().equals(StatusConstant.ENABLE)){
            user.setStatus(String.valueOf(StatusConstant.ENABLE));
        }else{
            user.setStatus(String.valueOf(StatusConstant.DISABLE));
        }
        return userMapper.save(user);
    }

    //修改用户
    @Override
    public Boolean update(User user) {
        //补齐参数
        if (user.getStatus().equals(StatusConstant.ENABLE)){
            user.setStatus(String.valueOf(StatusConstant.ENABLE));
        }else{
            user.setStatus(String.valueOf(StatusConstant.DISABLE));
        }
        user.setRoleCode(roleMapper.getRoleCodeById(user.getRoleId()));
        return userMapper.update(user);
    }

    @Override
    public Boolean delete(Integer userId) {
        return userMapper.delete(userId);
    }


}
